package com.firetornado.android.gowallajava;

import java.util.List;

import com.firetornado.android.gowallajava.ImageOps;
import com.firetornado.android.gowallajava.GowallaJavaAuth;
import com.firetornado.android.gowallajava.R;
import com.ginsberg.gowalla.dto.FullUser;
import com.ginsberg.gowalla.dto.User;
import com.ginsberg.gowalla.dto.VisitedSpot;
import com.ginsberg.gowalla.exception.GowallaException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This is an example of how to use gowalla-java within Android to call and display user details.
 * 
 * @author Ryan Crutchfield - rc6750@gmail.com
 * http://gowalla.com/users/rc6750
 *
 */

public class UsersMain extends Activity {
	
	private List<VisitedSpot> topspots;
	final Handler mHandler = new Handler();
	private FullUser userData = null;
	private List<User> friendsData = null;
		
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.usersmain);
        final ProgressDialog pd = ProgressDialog.show(this,"User Details","Loading....",true, false);
   		new Thread(new Runnable(){
       		public void run(){
       			try {
       	        	userData = GowallaJavaAuth.gowalla.getUser("rc6750");
       	        	friendsData = GowallaJavaAuth.gowalla.getUserFriends("rc6750");
       	        	topspots =  GowallaJavaAuth.gowalla.getTopSpots(userData.getId());
       			} catch (GowallaException e) {
       				// TODO Auto-generated catch block
       				e.printStackTrace();
       			}
       			mHandler.post(mUpdateResults);
        		pd.dismiss();
       		}
   		}).start();
	}
	
	final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi(userData,friendsData,topspots);
        }
    }; 
    
    final void updateResultsInUi(FullUser userData, List<User> friendsData, List<VisitedSpot> topspots)
    {
    	Drawable image = ImageOps.ImageOperations(userData.getImageUrl(),"image.jpg");		
    	ImageView imgView = (ImageView)findViewById(R.id.image1);
		imgView.setImageDrawable(image);
    			
		TextView user = (TextView)findViewById(R.id.userName);
		user.setText(userData.getName());
		
		TextView bio = (TextView)findViewById(R.id.userDescription);
		bio.setText("@" + userData.getTwitterUsername() + "\n" + 
				userData.getWebsite() + "\n" + userData.getBio());
		
		TextView stamps = (TextView)findViewById(R.id.userStamps);
		stamps.setText(Integer.toString(userData.getStampsCount()) + " Stamps");
		
		TextView items = (TextView)findViewById(R.id.userItems);
		items.setText(Integer.toString(userData.getItemsCount()) + " Items");
		
		TextView pins = (TextView)findViewById(R.id.userPins);
		pins.setText(Integer.toString(userData.getPinsCount()) + " Pins");
		
		TextView top = (TextView)findViewById(R.id.topspots);
		top.setText("Top Spots:\n");
		for (VisitedSpot spot : topspots)
		{
			top.append(spot.getName()+ " (" + Integer.toString(spot.getUserCheckinsCount()) + ")\n");
		}
		
	}	
	
}